package it.tcon.xbeedriver.services;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

import com.rapplogic.xbee.api.XBee;
import com.rapplogic.xbee.api.XBeeAddress16;
import com.rapplogic.xbee.api.XBeeException;
import com.rapplogic.xbee.api.XBeeRequest;
import com.rapplogic.xbee.api.zigbee.ZNetRemoteAtRequest;
import com.rapplogic.xbee.api.zigbee.ZNetTxRequest;

import it.tcon.xbeedriver.XBeeNode;
import it.tcon.xbeedriver.XBeeNodeService;
import it.tcon.xbeedriver.XBeeNodeParam.State;

public class Switch extends XBeeNodeService{
	private XBeeNode node;
	private XBee xbee;
	private CyclicBarrier barrier;
	
	public Switch(){
		super("Switch");
		
		this.barrier = barrier;
	}
	public void setNodeCom(XBeeNode node,XBee xbee){
		this.node = node;
		this.xbee = xbee;
	}

	@Override
	public String getService() {
		// TODO Auto-generated method stub
		ZNetTxRequest request = new ZNetTxRequest(xbee.getNextFrameId(), this.node.getAddress64(), XBeeAddress16.ZNET_BROADCAST, ZNetTxRequest.DEFAULT_BROADCAST_RADIUS, ZNetTxRequest.UNICAST_OPTION, new int[] { 0x01, 0x02 });
		//ZNetRemoteAtRequest sample = new ZNetRemoteAtRequest(XBeeRequest.DEFAULT_FRAME_ID, this.node.getAddress64(), XBeeAddress16.ZNET_BROADCAST, false, "IS");

		try {
			xbee.sendAsynchronous(request);
		} catch (XBeeException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		//try {
			while(node.getAnalog0().getState() == State.PARAM_UNSET){
				//Thread.currentThread().sleep(500);
			}
			int out[] = node.getAnalog0().getData();
			return out.toString();
		 /*}catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} */
		//return null;
		
	}

	@Override
	public void setService() {
		// TODO Auto-generated method stub
		
	}
	
	
}
